<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Useraddress extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\UserAddress();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lists()
    {
        $userId = $this->auth->id;
        $where = [
            'user_id' => $userId,
            'deletetime' => NULL
        ];

        $pageSize = input('page_size', 15);
        $lists = self::$model->where($where)->order('createtime desc')->paginate($pageSize);

        $this->success('收货地址列表', $lists);
    }

    /**
     * 添加地址
     */
    public function create()
    {
        $name = input('name');
        $mobile = input('mobile');
        $province = input('province');
        $city = input('city');
        $district = input('district');
        $address = input('address');
        if (!$name || !$mobile || !$district || !$address) {
            $this->error('收货人姓名，电话，地址缺一不可！');
        }

        $userId = $this->auth->id;
        $status = input('status', 0);
        if ($status == 1) {
            self::$model->where('user_id', $userId)->update(['default' => 0]);
        }

        $adata = [
            'user_id' => $userId,
            'name' => $name,
            'mobile' => $mobile,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'address' => $address,
            'default' => $status
        ];

        $res = self::$model->create($adata);
        if (!$res->id) {
            $this->error('添加地址失败！');
        }

        $this->success('添加地址成功！');
    }

    /**
     * 申请详情
     */
    public function read()
    {
        $id = input('id');
        if (!$id) {
            $this->error('参数缺失！');
        }

        $row_info = self::$model->where('id', $id)->find();
        if (!$row_info) {
            $this->error('该地址不存在！');
        }

        $this->success('地址详情', $row_info);
    }

    /**
     * 编辑地址
     */
    public function edit()
    {
        $id = input('id');
        if (!$id) {
            $this->error('参数缺失！');
        }

        $row_info = self::$model->where('id', $id)->find();
        if (!$row_info) {
            $this->error('该地址不存在！');
        }

        $name = input('name');
        $mobile = input('mobile');
        $province = input('province');
        $city = input('city');
        $district = input('district');
        $address = input('address');
        if (!$name || !$mobile || !$district || !$address) {
            $this->error('收货人姓名，电话，地址缺一不可！');
        }

        $userId = $this->auth->id;
        $status = input('status', 0);
        if ($status == 1) {
            self::$model->where('user_id', $userId)->update(['default' => 0]);
        }

        $adata = [
            'user_id' => $userId,
            'name' => $name,
            'mobile' => $mobile,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'address' => $address,
            'default' => $status
        ];

        $res = self::$model->where('id', $id)->update($adata);

        if (!$res) {
            $this->error('地址编辑失败！');
        }

        $this->success('地址编辑成功！');
    }

    /**
     * 删除地址
     */
    public function del()
    {
        $id = input('id');
        if (!$id) {
            $this->error('参数缺失！');
        }

        $row_info = self::$model->where('id', $id)->find();
        if (!$row_info) {
            $this->error('该地址不存在！');
        }

        $res = $row_info->where('id', $id)->update(['deletetime' => time()]);
        if (!$res) {
            $this->error('地址删除失败！');
        }

        $this->success('地址删除成功！');
    }
}
